'use strict'

process.env.BABEL_ENV = 'web'
process.env.IS_WEB = true

const path = require('path')
const CopyWebpackPlugin = require('copy-webpack-plugin')
var MiniCssExtractPlugin = require('mini-css-extract-plugin');
var ExtractTextPlugin = require('extract-text-webpack-plugin');

const HtmlWebpackPlugin = require('html-webpack-plugin')
var ScriptExtHtmlPlugin = require('script-ext-html-webpack-plugin');


let webConfig = {
    devtool: '#cheap-module-eval-source-map',
    entry: {
        app: [require.resolve('@babel/polyfill'),
        path.resolve(__dirname, '../src/lib/Math'),
        path.resolve(__dirname, '../src/index')]
    },
    module: {
        rules: [
            {
                test: /\.(js|tsx|jsx)$/,
                exclude: /node_modules/,
                loader: 'babel-loader'
            },
            {
                test: /\.(css|less)$/,
                loader: [
                    'style-loader',
                    "css-loader",
                    { loader: 'less-loader', options: { javascriptEnabled: true } }
                ]
            },
            {
                test: /\.(png|jpg|svg|gif)$/,
                loader: 'url-loader',
                options: {
                    limit: 8192
                }
            }
            // {
            //     test: require.resolve('../src/lib/middle'),
            //     loader: "MiddleLoader?platform=json"
            // },
        ]
    },
    plugins: [
        new HtmlWebpackPlugin({
            hash: true,
            inject: true,
            template: path.resolve(__dirname, '../public/index.html'),
            filename: 'index.html',
            chunks: ['vendor', 'common', 'app']
        }),
        new ScriptExtHtmlPlugin({
            defaultAttribute: 'defer'
        }),
        new MiniCssExtractPlugin({
            filename: '[name].style.css',
        })
    ],
    output: {
        path: path.resolve(__dirname, '../build'),
        filename: "[name].js",
    },
    resolve: {
        mainFields: ['browser', 'main'],
        alias: {
            'components': path.resolve(__dirname, '../src/components'),
            'style': path.resolve(__dirname, '../src/style/index.less')
        }
    }
}

module.exports = webConfig
